<template>
	<div class="layout-pd">
		<el-card shadow="hover">
			<div class="system-menu-search mb15">
				<!-- <el-input v-model="state.params.position_name" clearable size="default" placeholder="请输入职位名称" style="max-width: 180px"> </el-input> 
				 <el-button size="default" type="primary" class="ml10" @click="getTableData">
					<el-icon>
						<ele-Search />
					</el-icon>
					查询
				</el-button> -->
				<el-button size="default" type="success" @click="onAdd('add')">
					<el-icon>
						<ele-FolderAdd />
					</el-icon>
					新增
				</el-button>
			</div>
			<el-table
				:data="state.tableData.data"
				v-loading="state.tableData.loading"
				style="width: 100%"
				row-key="id"
				:default-expand-all="false"
				:tree-props="{ children: 'children' }"
			>
				<el-table-column prop="id" label="ID" show-overflow-tooltip></el-table-column>
				<el-table-column prop="picture_url" label="图片" show-overflow-tooltip>
					<template #default="scope">
						<OssImage :src="scope.row.picture_url" style="width: 100px"></OssImage>
					</template>
				</el-table-column>
				<el-table-column prop="create_time" label="创建时间" show-overflow-tooltip></el-table-column>
				<el-table-column label="操作" show-overflow-tooltip width="140">
					<template #default="scope">
						<!-- <el-button size="small" text type="primary" @click="onAdd('addChild', scope.row)">新增</el-button> -->
						<el-button size="small" text type="primary" @click="onEdit('edit', scope.row)">修改</el-button>
						<el-button size="small" text type="primary" @click="onTabelRowDel(scope.row)">删除</el-button>
					</template>
				</el-table-column>
			</el-table>
			<el-pagination
				@size-change="onHandleSizeChange"
				@current-change="onHandleCurrentChange"
				class="mt15"
				:pager-count="5"
				:page-sizes="[10, 20, 30]"
				v-model:current-page="state.params.page_num"
				background
				v-model:page-size="state.params.page_size"
				layout="total, sizes, prev, pager, next, jumper"
				:total="state.tableData.total"
			>
			</el-pagination>
		</el-card>
		<Dialog ref="dialogRef" @refresh="getTableData()" />
	</div>
</template>

<script setup lang="ts" name="systemMenu">
import { defineAsyncComponent, ref, onMounted, reactive } from 'vue';
import { ElMessageBox, ElMessage } from 'element-plus';
import { useBannerApi } from '/@/api/banner';
import OssImage from '/@/components/ossImage/index.vue';
// 引入组件
const Dialog = defineAsyncComponent(() => import('./dialog.vue'));
// 定义变量内容
const dialogRef = ref();
const state = reactive({
	tableData: {
		data: [],
		loading: true,
		total: 10,
	},
	params: {
		page_num: 1,
		page_size: 10,
		position_name: '',
		position_address: '',
		lang: 'zh',
	},
});
// 获取路由数据，真实请从接口获取
const getTableData = async () => {
	state.tableData.loading = true;
	const { result } = await useBannerApi().list(state.params);
	state.tableData.data = result;
	state.tableData.loading = false;
	state.tableData.total = result.length;
};
const onAdd = (type: string, row?: object) => {
	dialogRef.value.openDialog(type, row);
};
const onEdit = (type: string, row: object) => {
	dialogRef.value.openDialog(type, row);
};
// 分页改变
const onHandleSizeChange = (val: number) => {
	state.params.page_size = val;
	getTableData();
};
// 分页改变
const onHandleCurrentChange = (val: number) => {
	state.params.page_num = val;
	getTableData();
};
// 删除当前行
const onTabelRowDel = (row: any) => {
	ElMessageBox.confirm(`此操作将永久删除此条数据, 是否继续?`, '提示', {
		confirmButtonText: '删除',
		cancelButtonText: '取消',
		type: 'warning',
	})
		.then(async () => {
			await useBannerApi().del(row.id);
			ElMessage.success('删除成功');
			getTableData();
		})
		.catch(() => {});
};
// 页面加载时
onMounted(() => {
	getTableData();
});
</script>
